﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SCMS.Model;

namespace SCMS.CoreBusinessLogic.FleetIncidences
{
    public class FleetIncidenceService : IFleetIncidenceService
    {
        public bool IsIncidenceSaved(FleetMajorIncidence entity)
        {
            using (var db = new SCMSEntities())
            {
                db.FleetMajorIncidences.AddObject(entity);
                return db.SaveChanges() > 0 ? true : false;
            }
        }

        public bool IsFleetIncidenceEdited(FleetMajorIncidence entity)
        {
            using (var db = new SCMSEntities())
            {
                db.FleetMajorIncidences.Attach(entity);
                db.ObjectStateManager.ChangeObjectState(entity, System.Data.EntityState.Modified);
                return db.SaveChanges() > 0 ? true : false;
            }
        }

        public FleetMajorIncidence GetFleetIncidenceById(Guid incidenceId)
        {
            using (var db = new SCMSEntities())
            {
                return db.FleetMajorIncidences.FirstOrDefault(p => p.Id == incidenceId);
            }
        }

        public List<FleetMajorIncidence> GetFleetIncidencs(Guid cpId)
        {
            using (var db = new SCMSEntities())
            {
                return db.FleetMajorIncidences.Where(p => p.CountryProgramId == cpId).ToList();
            }
        }

        public bool IsFleetIncidenceDeleted(Guid incidenceId)
        {
            using (var db = new SCMSEntities())
            {
                db.FleetMajorIncidences.DeleteObject(db.FleetMajorIncidences.FirstOrDefault(p => p.Id == incidenceId));
                return db.SaveChanges() > 0 ? true : false;
            }
        }
    }
}
